package newXgame.utils
{
	import anshe.object.ExScroll;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class ExScrollUtils extends ExScroll
	{
		private var actSprite:Sprite;		//	受控制的 Sprite 对象
		private var maskSprite:Sprite;		//	用于遮照用的 Sprite 对象
		private var speedJump:Number;		//	点击向上/向下按钮点击后跳动的比例（默认5%）
		private var isHeightLitener:Boolean;	//	是否侦听高度的改变
		
		public function ExScrollUtils(hh:Number=0)
		{
			isHeightLitener = true;
			
			this.addEventListener(ExScroll.UP_CLICK, onUpClickEvent);
			this.addEventListener(ExScroll.DOWN_CLICK, onDownClickEvent);
			this.addEventListener(ExScroll.CHANGE_MOVE, onChangeMoveEvent);
		}
		
		//	向滑动条内注册控制对象及控制对象的遮照对象
		//		sprAct		被滑动条控制的对象
		//		sprMask		用于遮照的对象
		//		isHeight	是否是调节对象的高度
		//		sp			点击 向上/向下 按钮后每次跳动的次数
		public function SetSprites(sprAct:Sprite, sprMask:Sprite, isHeight:Boolean = true, sp:Number = 5):void
		{
			this.actSprite = sprAct;
			this.maskSprite = sprMask;
			this.speedJump = sp;
			this.isHeightLitener = isHeight;
			
			this.actSprite.addEventListener(MouseEvent.MOUSE_WHEEL, onChangePercent);
			
			if(!this.actSprite.mask)
			{
				this.actSprite.mask = this.maskSprite;
			}
		}
		
		private function onChangePercent(e:MouseEvent):void
		{
			this.percent -= e.delta;
			
			onChangeMoveEvent();
		}
		
		//	刷新滑动条的显示
		public function RushVisible():void
		{
			if(this.actSprite && this.maskSprite)
			{
				if(isHeightLitener)
				{
					this.visible = Boolean(this.actSprite.height > this.maskSprite.height);
				}
				else
				{
					this.visible = Boolean(this.actSprite.width > this.maskSprite.width);
				}
				
				if(!this.visible)
				{
					this.percent = 0;
				}
					
				onChangeMoveEvent();
			}
		}
		
		public override function ChangeHeight(hh:Number):void
		{
			var oldPer:Number = this.percent;
			
			super.ChangeHeight(hh);
			
			this.percent = oldPer;
			
			onChangeMoveEvent();
		}
		
		//=======================================================================
		
		private function onUpClickEvent(e:Event):void
		{
			this.percent -= this.speedJump;
			
			onChangeMoveEvent();
		}
		
		private function onDownClickEvent(e:Event):void
		{
			this.percent += this.speedJump;
			
			onChangeMoveEvent();
		}		
		
		private function onChangeMoveEvent(e:Event = null):void
		{
			if(this.actSprite && this.maskSprite)
			{
				var maxHeight:Number = this.GetMaxHeight();
				
				this.actSprite.visible = false;
							
				if(isHeightLitener)
				{
					this.actSprite.y = this.maskSprite.y - (maxHeight * this.percent/100);
				}
				else
				{
					this.actSprite.x = this.maskSprite.x - (maxHeight * this.percent/100); 
				}
				
				this.actSprite.visible = true;
			}
		}
		
		private function GetMaxHeight():Number
		{
			var maxHeight:Number;
		
			if(isHeightLitener)
			{
				maxHeight = this.actSprite.height - this.maskSprite.height;
			}
			else
			{
				maxHeight = this.actSprite.width - this.maskSprite.width; 
			}
			
			maxHeight = (maxHeight < 0) ? 0 : maxHeight;
			
			return maxHeight;
		}
	}
}